package com.rc.web.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.rc.common.constant.ApplyStatusType;
import com.rc.common.constant.Code;
import com.rc.common.constant.RoleEnum;
import com.rc.common.exception.BusinessException;
import com.rc.common.util.RabbitMqHelper;
import com.rc.common.util.SaltMD5Util;
import com.rc.pojo.dto.farmapply.AddFarmApplyDTO;
import com.rc.pojo.dto.farmapply.ApprovalFarmApplyDTO;
import com.rc.pojo.dto.farmapply.FarmApplyPageQuery;
import com.rc.pojo.po.Farm;
import com.rc.pojo.po.FarmApply;
import com.rc.pojo.po.User;
import com.rc.pojo.vo.farmapply.PageFarmApplyVO;
import com.rc.web.converter.FarmApplyConverter;
import com.rc.web.mapper.FarmApplyMapper;
import com.rc.web.service.FarmApplyService;
import com.rc.web.service.FarmService;
import com.rc.web.service.UserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 农场申请服务实现类
 * 提供农场申请相关的业务逻辑实现
 */
@Service
@RequiredArgsConstructor
public class FarmApplyServiceImpl extends ServiceImpl<FarmApplyMapper, FarmApply> implements FarmApplyService {
    // 农场申请转换器
    private final FarmApplyConverter applyConverter;
    // 用户服务
    private final UserService userService;
    // 农场服务
    private final FarmService farmService;
    // RabbitMQ工具类
    private final RabbitMqHelper rabbitMqHelper;

    /**
     * 添加农场申请
     * @param dto 农场申请DTO
     */
    @Override
    public void add(AddFarmApplyDTO dto) {
        // 检查手机号是否已申请
        FarmApply farmApply = mapper.selectOneByPhone(dto.getPhone());
        if (farmApply != null){
            throw new BusinessException(Code.APPLY_EXITED);
        }

        // 转换DTO为PO并保存
        farmApply = applyConverter.addDTO2Po(dto);
        this.save(farmApply);
    }

    /**
     * 审批农场申请
     * @param dto 审批DTO
     */
    @Override
    public void approval(ApprovalFarmApplyDTO dto) {
        // 获取申请记录
        FarmApply farmApply = this.getById(dto.getId());
        if (farmApply.getStatus() != ApplyStatusType.APPROVAL){
            throw new BusinessException(Code.APPROVAL);
        }

        Boolean pass = dto.getPass();

        if (!pass){
            // 审核不通过处理
            if (StringUtils.isBlank(dto.getReason())){
                throw new RuntimeException("审核不通过必须填写审核不通过原因");
            }
            farmApply.setStatus(ApplyStatusType.UN_PASS);
            farmApply.setReason(dto.getReason());
            this.updateById(farmApply);
        }else {
            // 审核通过处理
            // 1、更新审核记录
            farmApply.setStatus(ApplyStatusType.PASS);
            this.updateById(farmApply);

            // 2、向农庄表插入农庄信息
            Farm farm = Farm.builder()
                    .name(farmApply.getFarmName())
                    .address(farmApply.getAddress())
                    .build();
            farmService.save(farm);

            // 3、向用户表插入农庄管理员
            String password = String.valueOf(ThreadLocalRandom
                    .current()
                    .nextInt(100000, 1000000));

            User user = User.builder()
                    .name(farmApply.getName())
                    .phone(farmApply.getPhone())
                    .role(RoleEnum.FARM_ADMIN)
                    .password(SaltMD5Util.generatePassword(password))
                    .farmId(farm.getId())
                    .build();
            userService.save(user);
        }
        // 4、TODO 发送短信告知农庄管理员
    }

    /**
     * 分页查询农场申请
     * @param dto 分页查询条件
     * @return 分页结果
     */
    @Override
    public Page<PageFarmApplyVO> getPage(FarmApplyPageQuery dto) {
        return mapper.selectPage(dto);
    }
}
